grammar SIMPLE-UNTYPED;
options
{
	backtrack = true;
	output = AST;
}

id 	: ID 
	;
intR 	: INT 
	;
stringR : STRING 
	;
bool	: BOOL
	;

start : stmts
	;

ids 	:	id ',' ids 
	|	id
	;

exps 	:	exp ',' exps
	|	exp
	;

decl : 'var' exps ';' 
     | 'function' id '(' ids ')' stmt 
     | 'function' id '(' ')' stmt 
     ;

exp_1  	: 
	 '(' exp ')' 
	| '++' exp 
	| '-' exp 
	| 'sizeOf' '(' exp ')' 
	| 'read' '(' ')' 
	| 'not' exp
	| intR 
	| bool 
	| id 
	| stringR
	;		   
exp 	: exp_1 '[' exps ']' 
	| exp_1 '(' exps ')' 
	| exp_1 '*' exp 
	| exp_1 '/' exp 
	| exp_1 '%' exp 
	|  exp_1'+' exp 
	|  exp_1 '-' exp 
	|  exp_1 '<' exp 
	| exp_1 '<=' exp 
	|  exp_1 '>' exp 
	| exp_1 '>=' exp 
	| exp_1 '==' exp 
	| exp_1 '!=' exp 
	| exp_1 'and' exp 
	| exp_1 'or' exp 
	| exp_1 '=' exp 
	| exp_1
	;	 
stmt : '{' stmts '}'
     | '{' '}'
     | exp ';'
     | 'if' exp 'then' stmt 'else' stmt
     | 'if' exp 'then' stmt
     | 'while' exp 'do' stmt
     | 'for' id '=' exp 'to' exp 'do' stmt
     | 'return' exp ';'
     | 'return' ';'
     | 'write' '(' exp ')' ';'
     | 'try' stmt 'catch' '(' id ')' stmt
     | 'throw' exp ';'
     | 'spawn' stmt	
     | 'acquire' exp ';' 
     | 'release' exp ';'
     | 'rendezvous' exp ';' 
     | 'print' '(' exps ')' ';' 
      ;


stmts : decl stmts*
	| stmt stmts*
                 ;	

BOOL 	:	'true'
	|	'false'
	;

STRING 	: '"' .* '"'
	;
ID  : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* 
    ;

INT	: '-'?('0'|'1'..'9')('0'..'9')*
	;
WS  :  (' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;}
    ;
   
